<template>
  <div>
    <!-- 导航栏 -->
    <van-nav-bar
      title="登录"
      left-arrow
      @click-left="onClickLeft"
    />
    <!-- 表单 -->
    <van-form
      @submit="onSubmit"
      ref="form"
    >
      <van-cell-group inset>
        <van-field
          type="number"
          maxlength="11"
          v-model="user.mobile"
          name="mobile"
          placeholder="请填写手机号"
          :rules="loginRules.mobile"
        ><i
            slot="left-icon"
            class="iconfont icon-shouji"
          > </i> </van-field>
        <van-field
          type="number"
          maxlength="6"
          v-model="user.code"
          name="验证码"
          placeholder="请输入验证码"
          :rules="loginRules.code"
        >
          <i
            slot="left-icon"
            class="iconfont icon-yanzhengma"
          > </i>
          <template #button>
            <van-count-down
              v-if="isShowCount"
              :time="1000*5"
              format="ss s"
              @finish="isShowCount=false"
            />
            <van-button
              @click="sendSms"
              native-type="button"
              v-else
              round
              size="mini"
              style=" background: #ededed;color:#9b9b9b"
            >发送验证码</van-button>
          </template></van-field>
      </van-cell-group>
      <div style="margin: 16px;">
        <van-button
          block
          class="login-btn"
          native-type="submit"
        >
          登录
        </van-button>
      </div>
    </van-form>
  </div>
</template>
<script>
import { login, sendSms } from '@/api/user.js'
export default {
  data () {
    return {
      user: {
        mobile: '13911111111',
        code: '246810'
      },
      loginRules: {
        mobile: [
          { required: true, message: '请输入手机号' },
          { pattern: /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/, message: '手机号格式错误' }
        ],
        code: [
          { required: true, message: '请填写验证码' },
          { pattern: /^\d{6}$/, message: '验证码格式错误' }
        ]
      },
      isShowCount: false
    }
  },
  methods: {
    // 登录事件
    async onSubmit () {
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true,
        duration: 0
      })
      try {
        const res = await login(this.user)
        console.log(res)
        this.$store.commit('setToken', res.data.data)
        this.$router.back()
        this.$toast.success('登录成功')
      } catch (err) {
        if (err.response && err.response.status === 400) {
          this.$toast.fail('手机号或验证码错误')
        } else {
          this.$toast.fail('登录失败,请稍后重试')
        }
      }
    },
    // 发送验证码
    sendSms: async function () {
      try {
        await this.$refs.form.validate('mobile')
      } catch (err) {
        return this.$toast.fail('请填写正确手机号')
      }
      try {
        await sendSms(this.user.mobile)
        this.$toast('发送成功')
        this.isShowCount = true
      } catch (err) {
        if (err.response && err.response.status === 429) {
          this.$toast('发送太频繁，请稍后再试')
        } else {
          this.$toast('发送失败，请稍后再试')
        }
      }
    },
    onClickLeft () {
      this.$router.go(-1)
    }
  },
  name: 'login'
}
</script>
<style lang="less" scoped>
.login-btn {
  background: #6db4fb;
  color: #fff;
}
/deep/ .van-nav-bar {
  background: #3296fa;
  .van-nav-bar__title {
    color: #fff !important;
  }
}
.iconfont {
  font-size: 39px;
}
.van-field__control {
  height: 60px;
}
.van-count-down {
  height: 63px;
}
</style>
